package com.fyj.service;

import com.fyj.pojo.MyMission;
import com.fyj.pojo.ResultCode;
import com.fyj.pojo.User;
import com.fyj.repository.MyMissionRepository;
import com.fyj.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 钰杰
 * @className AdminService
 * @description VX:FengYJ996
 * @date 2019/12/610:57
 */
@Service
public class AdminService {

    @Autowired
    private MyMissionRepository myMissionRepository;

    @Autowired
    private UserRepository userRepository;

    /**
     * 审核任务
     *
     * @param missionids like '1,2,3,4,5,...'
     * @param updtsatus  like '0,1,1,2,2,...' 不操作为0值  3 为取消任务
     * @param userid
     * @return
     */
    public int examineMission(String missionids, String updtsatus, Integer userid) {
        String[] missionid = missionids.split(",");
        String[] updStatus = updtsatus.split(",");
        int count = 0;
        for (int i = 0; i < missionid.length; i++) {
            count = myMissionRepository.updMissionStatus(userid, Integer.parseInt(updStatus[i]), Integer.parseInt(missionid[i]));
        }
        if (count == 0) {
            return ResultCode.ERROR_ADMIN_EXAMINE_MISSION_CODE;

        }
        return count;
    }

    /**
     * 获取所有任务信息
     *
     * @return
     */
    public List<MyMission> getAllMission() {
        return myMissionRepository.findAll();
    }

    //按权重排序
    public List<MyMission> getMissionByLevel(){
        return myMissionRepository.findAllByLevelDesc();
    }

    //按时间排序
    public List<MyMission> getMissionByCdate(){
        return myMissionRepository.findAllByCdateAsc();
    }

    //修改用户
    public void updUser(User user){
        userRepository.save(user);
    }

    //用户列表
    public List<User> findUserList(){
        return userRepository.findAll();
    }
}
